package lab03.Test08;
import com.mysql.cj.x.protobuf.MysqlxExpr;
import java.io.*;
public class Test {
    public static void main(String[] args)throws IOException {
        File f1 = new File("D:/file1.txt");
        File f2 = new File("D:/file2.txt");
        File f3 = new File("D:/file3.txt");
        OperatorFile of=new OperatorFile();
        of.write(f1);
        of.read(f1);
        of.write(f2);
        of.read(f2);
        of.mergeFile(f1, f2, f3);
        of.read(f3);
        of.closeOperate();
    }
}
    class OperatorFile{
    private BufferedWriter bw = null;
    private BufferedReader br = null;
    private FileInputStream fis1 = null, fis2 = null;
    DataOutputStream dos = null;
        public  void write(File f)throws IOException{
            //写文件
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));
            br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println(f.toString() + "\n输入您要写入的内容:");
            String str = br.readLine();
            bw.write(str);
            bw.flush();
            System.out.println("写文件完成！\n");}
        public  void read(File f)throws IOException{
            //读文件
            br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
            System.out.println(f.toString() + "\t的内容如下:");
            String str = br.readLine();
            System.out.println(str);
            System.out.println("\n读文件完成！");}
        public  void mergeFile(File f1, File f2, File f3)throws IOException{
            //把文件f1和文件f2合并到文件f3
            fis1 = new FileInputStream(f1);
            fis2 = new FileInputStream(f2);
            dos = new DataOutputStream(new FileOutputStream(f3));
            int temp1 = fis1.read();
            int temp2 = fis2.read();
            while((temp1 != -1) && (temp2 != -1)){
                dos.write(temp1);
                dos.write(temp2);
                temp1 = fis1.read();
                temp2 = fis2.read();}
            if(temp1 == -1){
                while(temp2 != -1){
                    dos.write(temp2);
                    temp2 = fis2.read(); }
            } if(temp2 == -1){
                while(temp1 != -1){
                    dos.write(temp1);
                    temp1 = fis1.read();}}
            dos.flush();}
        public void closeOperate() throws IOException{
            if(br != null) br.close();
            if(bw != null) bw.close();
            if(dos != null) dos.close();
            if(fis1 != null) fis1.close();
            if(fis2 != null) fis2.close();
        }
}



